﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuongLaiDangCho.DTO;
using System.Data.OleDb;
using System.Data;

namespace TuongLaiDangCho.DAO
{
    class NhatKiDAO
    {
        public static string LoadDiary(NhatKiDTO diary)
        {
            string sql = "Select NoiDung From NhatKi where NgayThang=#" + diary.Ngay.ToShortDateString() + "#";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            return "";
        }
        public static void InsertDiary(NhatKiDTO diary)
        {
            string sql = "Insert into NhatKi(NgayThang, NoiDung) values(#" + diary.Ngay.ToShortDateString() + "#,'" + diary.Noidung + "')";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(sql,cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }
        public static void UpdateDiary(NhatKiDTO diary)
        {
            string sql = "Update NhatKi set NoiDung='" + diary.Noidung + "' where NgayThang=#" + diary.Ngay.ToShortDateString() + "#";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static void DeleleDiary(NhatKiDTO diary)
        {
            string sql = "Delete From NhatKi Where NgayThang=#" + diary.Ngay.ToShortDateString() + "#";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbCommand cm = new OleDbCommand(sql, cn);
            cm.ExecuteNonQuery();
            cn.Close();
        }

        public static bool IsDate(NhatKiDTO diary)
        {
            string sql = "Select * From NhatKi Where NgayThang=#" + diary.Ngay.ToShortDateString() + "#";
            OleDbConnection cn = DataProvider.OpenCn();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
    }
}
